var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pics:[],
    picimg:"",
    content:"",
    backpage:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.backpage){
      this.setData({ backpage: options.backpage})
    }
  },

  //长按删除图片
  bindlongpressimg: function (e) {
    var that = this;
    var newpics = that.data.pics;
    wx.showModal({
      title: '提示',
      content: '是否删除！',
      success: function (res) {
        if (res.confirm) {
          newpics.splice(e.currentTarget.dataset.id, 1);
          that.setData({ pics: newpics })
        } else if (res.cancel) {
          console.log('用户点击取消')
          return
        }
      }
    })
  },

  //输入的文本
  getsay:function(e){
    this.setData({ content: e.detail.value})
  },

  //发布动态
  sendBlogComment:function(){
    var that = this;
    let cardId = wx.getStorageSync("cardId");
    var info = {
      uid: app.globalData.userInfo.Uid,
      storeid: app.globalData.storeId,
      content: that.data.content,
      imgs: that.data.picimg
    }
    wx.request({
      url: app.globalData.host +  '/api/blog/AddBlog',
      data: info,
      method: 'POST',
      success: function(res) {
        wx.setStorageSync("cardId", cardId);
        if(res.data.Code == 0){
          wx.showToast({
            title: '发布成功！',
          })
          if (that.data.backpage == "states"){
            wx.reLaunch({
              url: '/pages/weijieke/states/index',
            })
          }else{
            wx.redirectTo({
              url: '/pages/h5page/index?url=' + encodeURI('friendState.html'),
            })
          }
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  chooseUploadImg: function (e) { //这里是选取图片的方法
    var that = this;
    var cardId = wx.getStorageSync("cardId");
    var pics = that.data.pics;
    if(pics.length >=9){
      wx.showToast({
        title: '最多选择9张！',
      })
      return;
    }
    wx.chooseImage({
      count: 9, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        wx.setStorageSync("cardId", cardId);
        var imgs = res.tempFilePaths;
        for(var i = 0; i < imgs.length; i++){
          pics.push(imgs[i])
        }
        that.setData({ pics: pics });
      },
    })
  },

  companyUploadImg: function () { //这里触发图片上传的方法
    var that = this;
    var cardId = wx.getStorageSync("cardId");
    var pics = that.data.pics;
    if (that.data.content == "" && pics.length == 0) {
      wx.showToast({
        title: '请输入内容！',
      })
      return;
    }
    if (pics.length > 9) {
      wx.showToast({
        title: '最多选择9张！',
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: '是否确认提交！',
      success: function (res) {
        wx.setStorageSync("cardId", cardId);
        if (res.confirm) {
          if (pics.length>0){
            that.uploadimg({
              url: app.globalData.host + '/api/tool/upload?operation=uploadblogimage&storeid=' + app.globalData.storeId, //这里是你图片上传的接口
              // url: app.globalData.host + '/api/tool/upload?operation=savecardavater', //这里是你图片上传的接口(照片墙)
              path: pics, //这里是选取的图片的地址数组
            });
          }else{
            that.sendBlogComment();
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
          return
        }
      }
    })
  },

  //多张图片上传
  uploadimg: function (data) {
    wx.showLoading({
      title: '提交中...',
      mask: true,
    })
    var that = this, i = data.i ? data.i : 0, success = data.success ? data.success : 0, fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'fileData',
      formData: null,
      success: (resp) => {
        wx.hideLoading();
        success++;
        var str = resp.data
        var pic = JSON.parse(str);
        var pic_name = pic.Data;
        if(i > 0){
          that.setData({ picimg: that.data.picimg + "," + pic_name})
        }
        if(i == 0){
          that.setData({
            picimg: pic_name
          })
        }
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        i++;
        if (i == data.path.length) { //当图片传完时，停止调用     
          console.log('成功：' + success + " 失败：" + fail);
          that.sendBlogComment();
        } else { //若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  },

  cancelBtn:function(){
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  }
  

})